package org.redmaple.core.template.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.redmaple.core.retrieving.PropertyNameResolver;
import org.redmaple.core.retrieving.RedmapleCriteria;
import org.redmaple.core.retrieving.Sort;
import org.redmaple.core.template.dao.IGenericDao;
import org.redmaple.core.template.model.BaseModel;
import org.redmaple.core.template.service.IGenericService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 一般化Service基类
 * 
 * @author Xiangyun
 *
 * @param <T>
 */
public abstract class GenericServiceImpl<T extends BaseModel> implements IGenericService<T> {

	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	protected IGenericDao<T> dao;
	
	/**
	 * Service级参数解析器
	 */
	private PropertyNameResolver servicePropertyResolver;
	
	public GenericServiceImpl() {
		logger.debug("初始化成功 " + getClass().getName());
	}
	
	/**
	 * 获取dao对象
	 * 
	 * @return dao对象
	 */
	public IGenericDao<T> getDao() {
		return dao;
	}

	/**
	 * 设置Daoe对象。<br/>
	 * 这个方法要由具体类来实现，格式如下：
	 * 
	 * <br/>@Resource(name = "****")<br/>
	 * public void setDao(IGenericDao&lt;T&gt; dao){<br/>
	 * &emsp;this.dao = dao;<br/>
	 * }<br/>
	 * <br/>
	 *                其中****就是你要注入的Dao对象
	 * 
	 * @param service
	 */
	protected abstract void setDao(IGenericDao<T> dao);
	
	/**
	 * 获取参数解析器
	 * 
	 * @return 返回获取到的参数解析器
	 */
	public PropertyNameResolver getServicePropertyResolver() {
		return servicePropertyResolver;
	}
	
	@Resource(name="servicePropertyResolver")
	public void setServicePropertyResolver(
			PropertyNameResolver servicePropertyResolver) {
		this.servicePropertyResolver = servicePropertyResolver;
	}
	
	public T save(T entity) {
		return dao.save(entity);
	}

	public T update(T entity) {
		return dao.update(entity);
	}

	public void delete(String id) {
		dao.delete(id);
	}
	
	public void deletes(List<String> ids) {
		dao.deletes(ids);
	}

	public void delete(T entity) {
		dao.delete(entity);
	}

	public T findOne(String id) {
		return dao.findOne(id);
	}

	public boolean exists(String id) {
		return dao.exists(id);
	}

	public List<T> findAll() {
		return dao.findAll();
	}

	public List<T> findListByCondition(Map<String, Object> filterPropertyMap,
			List<Sort> sortValues) {
		RedmapleCriteria redmapleCriteria = new RedmapleCriteria();
		redmapleCriteria.add(filterPropertyMap, servicePropertyResolver);
		return dao.findListByCondition(redmapleCriteria, sortValues);
	}
	
}
